package in.hitmedia.test.stack;

public class TowersOfHanoi 
{
	
	
	public TowersOfHanoi()
	{
		
	}
	
	private boolean add(Long disk, Stack<Long> destination)
	{
		if (destination.peek() == null || destination.peek() >= disk)
		{
			destination.push(disk);
			return true;
		}
		return false;
	}
	
	private void moveToTop(Stack<Long> source, Stack<Long> destination)
	{
		Node<Long> node = source.pop();
		if (!add(node.getData(), destination))
		{
			destination.push(node.getData());
		}
	}
	
	public void moveDisks(int n, Stack<Long> source, Stack<Long> destination, Stack<Long> buffer )
	{
		if (n > 0)
		{
			moveDisks(n-1, source, buffer, destination);
			//System.out.println("Source: "+source.toString()+" \t Buffer: "+buffer.toString()+ "\t Destination: "+destination.toString());
			
			moveToTop(source, destination);
			System.out.println("Source: "+source.toString()+" \t Buffer: "+buffer.toString()+ "\t Destination: "+destination.toString());
			
			moveDisks(n-1, buffer, destination, source);
			//System.out.println("Source: "+source.toString()+" \t Buffer: "+buffer.toString()+ "\t Destination: "+destination.toString());
		}
	}
	
	public static void main(String args[])
	{
		TowersOfHanoi toi = new TowersOfHanoi();
		Stack<Long> tower1 = new Stack<Long>(); //source
		Stack<Long> tower2 = new Stack<Long>(); //buffer
		Stack<Long> tower3 = new Stack<Long>();//destination
		
		tower1.push(4L);
		tower1.push(3L);
		tower1.push(2L);
		tower1.push(1L);
		System.out.println("Source: "+tower1.toString());
		
		
		toi.moveDisks(4, tower1, tower3, tower2);
		System.out.println("Source: "+tower1.toString()+" \t Buffer: "+tower2.toString()+ "\t Destination: "+tower3.toString());
	}
}
